<?php
namespace Api\Model;
use Think\Model;

/**
 * Class ScoreModel
 * @package Api\Model
 * 分数结算模型
 */
class ScoreModel extends Model {
    /**
     *
     */
    public function add_score_data($ip,$cname,$score,$content=''){
        if(!$ip || !$score){
            return model_fail();
        }
        $data = $this->where("ip",$ip)->find();
        if($data){
            $save_data = [
                "id"=>$data['id'],
                "cname"=>$cname,
                "score"=>$score,
                "add_time"=>time()
            ];
            if($content){ $save_data['content'] = $content; }
            if($score > $data['max_score']){
                $save_data['max_score'] = $score;
            }
            $result = $this->save($save_data);
            return $result?model_success($result):model_fail($result);
        }
        else{
            $result = $this->add([
                "ip"=>$ip,
                "cname"=>$cname,
                "score"=>$score,
                "max_score"=>$score,
                "content"=>$content,
                "add_time"=>time(),
            ]);
            return $result?model_success($result):model_fail($result);
        }


    }
}